/* 微信支付 */

/**
* 创建WxPay命名空间，WxPay提供的内容都在此空间下
*/
var WxPay = {
    version: '1.0'
};

/**
* ------------------工具集合-------------------
*/
WxPay.utils = {

};

// 微信统一下单
//orderType 1:酒店 2：线路 3：门票
WxPay.utils.UnifiedoOrder = function (orderno, bussinessType, callback) {
    //处理参数
    var account = iss_localStorage.get("yuqing_appAccount"); // 全局变量
    var openid = iss_localStorage.get("yuqing_appOpenId"); // 全局变量
    // var openid='oX816v34ifGyjDa9nqWIS4AVniWo';

    //myApp.showPreloader(); 
    $$.ajax({
        url: ISS_Constants.Url.const_weixin_unifiedorder,
        data: {
            "conditionParm": "{'AccountGuid':'" + account + "','OpenId':'" + openid +
                                 "','OrderNo':'" + orderno + "','BussinessType':'" + bussinessType + "'}"
        }, 
        crossDomain: true,
        dataType: "json",
        async: false,
        success: function (data, status) {
            //myApp.hidePreloader();
            if (data && data.success && data.datas) {
                var jsPayObject = data.datas;
                WeixinJSBridge.invoke(
                        'getBrandWCPayRequest', {
                            "appId": jsPayObject.appId, //公众号名称，由商户传入
                            "timeStamp": jsPayObject.timeStamp, //时间戳，自1970年以来的秒数
                            "nonceStr": jsPayObject.nonceStr, //随机串
                            "package": jsPayObject.package, //prepay_id,预付款单号
                            "signType": jsPayObject.signType, //微信签名方式:
                            "paySign": jsPayObject.paySign//微信签名
                        },

                        function (res) {
                            var result = false;
                            if (res.err_msg == "get_brand_wcpay_request:ok") {
                                result = true;
                                myApp.alert('支付成功!');
                            }
                            else {
                                myApp.alert('支付失败!');
                            }
                            if (typeof callback === "function") {
                                callback(result);
                            }
                        }
                    );
            }
            else {
                myApp.alert('支付失败!');
                if (typeof callback === "function") {
                    callback(false);
                }
            }
        },
        error: function (xhr, status) {
            //myApp.hidePreloader();
            myApp.alert('支付失败!');
            if (typeof callback === "function") {
                callback(false);
            }
        }
    });
}

// 微信退款
//orderType 1:酒店 2：线路 3：门票
WxPay.utils.RefundOrder = function (orderNo, orderType) {
    var account = iss_localStorage.get("yuqing_appAccount"); // 全局变量
    var openid = iss_localStorage.get("yuqing_appOpenId"); // 全局变量

    if (!hasUserId("请申请或绑定平台账号")) {
        myApp.loginScreen();
        return;
    }

    myApp.showPreloader();
    var bs64 = new Base64();
    $$.ajax({
        url: ISS_Constants.Url.const_weixin_refundorder,
        data: {
            "conditionParm": "{\"AccountGuid\":\"" + account +
                "\",\"OpenId\":\"" + openid +
                "\",\"OrderNo\":\"" + orderNo +
                "\",\"RefundFee\":\"" + 1 +
                "\",\"TotalFee\":\"" + 1 +
                "\",\"OrderType\":\"" + orderType + "\"}"
            //                "\",\"RefundFee\":\"" + parseInt(refundFee*100) +
            //                "\",\"TotalFee\":\"" + parseInt(totalFee*100) + "\"}"
        },
        crossDomain: true,
        dataType: "json", 
        async: false,
        success: function (data, status) {
            myApp.hidePreloader();
            if (data && data.success == true) {
                myApp.alert('退款成功!');
                //                mainView.router.loadPage("user/orderList.html");
            }
            else {
                myApp.alert('退款失败!');
            }
        },
        error: function (xhr, status) {
            myApp.hidePreloader();
            myApp.alert('退款失败!');
        }
    });
}

//扫码支付-图片流
//orderType 1:酒店 2：线路 3：门票
WxPay.utils.BizPayImg = function (orderNo, orderType) {
    var account = iss_localStorage.get("yuqing_appAccount"); // 全局变量
    //account = "b760689cb63a4a92834d6f14173bc293";
    var openid = iss_localStorage.get("yuqing_appOpenId"); // 全局变量

    //处理data.datas  -- 图片url
    if (orderType == 2) {
        $$("#BizImgHotel").attr("src", ISS_Constants.Url.const_weixin_bizpay_showimg + "accout=" + account
        + "&openid=" + openid + "&orderno=" + orderNo + "-" + orderType);
    }
}

WxPay.utils.CheckWxVer = function () {
    var message = '';
    var wechatInfo = navigator.userAgent.match(/MicroMessenger\/([\d\.]+)/i);
    if (!wechatInfo) {
        message = "本活动仅支持微信";
    } else if (wechatInfo[1] < "5.0") {
        message = "本活动仅支持微信5.0以上版本";
    }
    return message;
}

WxPay.utils.TicketPanicByingOrder = function (orderno, callback) {
    myApp.showPreloader();
    var obj = new obj();
    obj.OrderNo = orderno;
    obj.OrderType = 4; //公众号自营

    $$.ajax({
        url: ISS_Constants.Url.const_weixin_unifiedorder,
        data: {
            "conditionParm": JSON.stringify(obj)
        },
        crossDomain: true,
        dataType: "json",
        async: false,
        success: function (data, status) {
            myApp.hidePreloader();
            if (data && data.success && data.datas) {
                var jsPayObject = data.datas;
                WeixinJSBridge.invoke(
                        'getBrandWCPayRequest', {
                            "appId": jsPayObject.appId, //公众号名称，由商户传入
                            "timeStamp": jsPayObject.timeStamp, //时间戳，自1970年以来的秒数
                            "nonceStr": jsPayObject.nonceStr, //随机串
                            "package": jsPayObject.package, //prepay_id,预付款单号
                            "signType": jsPayObject.signType, //微信签名方式:
                            "paySign": jsPayObject.paySign//微信签名
                        },

                        function (res) {
                            if (res.err_msg == "get_brand_wcpay_request:ok") {
                                myApp.hidePreloader();
                                myApp.alert('支付成功!');
                                if (callback) {
                                    callback("true");
                                }
                            }
                        }
                    );
            }
            else {
                myApp.hidePreloader();
                myApp.alert('支付失败!');
                if (callback) {
                    callback("false");
                }
            }
        },
        error: function (xhr, status) {
            myApp.hidePreloader();
            myApp.alert('支付失败!');
            if (callback) {
                callback("false");
            }
        }
    });
}